package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.feign.list.SearchFeignClient;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.domain.CategoryView;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gmall.product.service.SkuInfoService;
import org.mapstruct.Qualifier;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
    implements SkuInfoService{
    @Autowired
    SkuInfoMapper skuInfoMapper;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    RBloomFilter<Object> skuIdBloom;
    @Autowired
    SearchFeignClient searchFeignClient;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;
    @Autowired
    CategoryViewMapper categoryViewMapper;
    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;


    /**
     * 商品上下架
     * @param skuId
     * @param status
     */
    @Override
    public void upOrDown(Long skuId, int status) {
        skuInfoMapper.updateSkuStatus(skuId,status);
        if (status==1){
            //给ES ,保存|删除数据 .查到当前sku的详细信息,封装成Goods
            Goods goods = this.getSkuInfoForSearch(skuId);

            //保存到es
            searchFeignClient.saveGoods(goods);

        }
        if (status==0){
            //下架,从ES中删除
            searchFeignClient.deleteGoods(skuId);
        }

    }

    @Transactional
    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1.SkuInfo基本信息保存到sku_info
        skuInfoMapper.insert(skuInfo);
        //sku自增id
        Long skuInfoId = skuInfo.getId();
        //给布隆保存skuId
        skuIdBloom.add(skuInfoId);
        //2.skuAttrValueList保存到skuAttrValueList
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        for (SkuAttrValue skuAttrValue : skuAttrValueList) {
            skuAttrValue.setSkuId(skuInfoId);
        }
        skuAttrValueService.saveBatch(skuAttrValueList);
        //3.skuSaleAttrValueList保存到sku_sale_attr_value
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        for (SkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
            skuSaleAttrValue.setSkuId(skuInfoId);
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
        }
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);
        //4.skuImageList 保存到sku_image
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        for (SkuImage skuImage : skuImageList) {
            skuImage.setSkuId(skuInfoId);

        }
        skuImageService.saveBatch(skuImageList);

    }

    @Override
    public BigDecimal getSkuPrice(Long skuId) {

        BigDecimal bigDecimal=skuInfoMapper.getSkuPrice(skuId);
        return bigDecimal;
    }
    /**
     * 查询sku对应的spu的销售属性和值
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSkuSaleAttrAndValue(Long skuId) {
        List<SpuSaleAttr> saleAttrs =spuSaleAttrMapper.getSkuSaleAttrAndValue(skuId);

        return saleAttrs;
    }

    @Override
    public List<Long> getAllSkuIds() {


        return skuInfoMapper.getSkuIds();
    }

    @Override
    public Goods getSkuInfoForSearch(Long skuId) {
        Goods goods = new Goods();



        //查询sku信息
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date()); //上架时间

        //品牌
        BaseTrademark baseTrademark = baseTrademarkMapper.selectById(skuInfo.getTmId());
        goods.setTmId(baseTrademark.getId());
        goods.setTmName(baseTrademark.getTmName());
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());

        //分类信息
        QueryWrapper<CategoryView> categoryViewQueryWrapper = new QueryWrapper<>();
        categoryViewQueryWrapper.eq("category3_id", skuInfo.getCategory3Id());
        CategoryView categoryView = categoryViewMapper.selectOne(categoryViewQueryWrapper);

        goods.setCategory1Id(categoryView.getCategory1Id());
        goods.setCategory1Name(categoryView.getCategory1Name());
        goods.setCategory2Id(categoryView.getCategory2Id());
        goods.setCategory2Name(categoryView.getCategory2Name());
        goods.setCategory3Id(categoryView.getCategory3Id());
        goods.setCategory3Name(categoryView.getCategory3Name());
        //热度分
        goods.setHotScore(0L);

        //当前平台属性的名称和值
        List<SearchAttr> attrs=baseAttrInfoMapper.getSkuBaseAttrNameAndValue(skuId);
        goods.setAttrs(attrs);

         return goods;
    }
}




